local zishucup__ruwo = fk.CreateSkill{
  name = "zishucup__ruwo",
}

Fk:loadTranslationTable{
  ["zishucup__ruwo"] = "入握",
  [":zishucup__ruwo"] = "出牌阶段限一次，你可以视为使用【借刀杀人】，然后将因此获得的武器牌置入一名角色的装备区。",

  ["#zishucup__ruwo"] = "入握：是否视为使用【借刀杀人】，且结算后分配武器牌？",
  ["#zishucup__ruwo-delay"] = "入握：请将此法获得的武器牌 %arg 置入一名角色装备区内！",

  ["$zishucup__ruwo1"] = "青龙偃月不折，何以铸我固陵刀？ ",
  ["$zishucup__ruwo2"] = "尔所倚仗，长刀快马，今已入我之握！",
}

zishucup__ruwo:addEffect("viewas", {
  anim_type = "support",
  prompt = "#zishucup__ruwo",
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local c = Fk:cloneCard("collateral")
    c.skillName = zishucup__ruwo.name
    return c
  end,
  before_use = function(self, player, use)
    use.extra_data = use.extra_data or {}
    use.extra_data.zishucup__ruwo_user = player.id
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(zishucup__ruwo.name, Player.HistoryPhase) == 0
  end,
  enabled_at_response = Util.FalseFunc,
})

zishucup__ruwo:addEffect(fk.CardUseFinished, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
   return not player.dead and table.contains(data.card.skillNames, zishucup__ruwo.name)
    and data.extra_data and data.extra_data.zishucup__ruwo_user == player.id and data.extra_data.zishucup__ruwo_weapon
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    if data.extra_data then
      for _, d in ipairs(data.extra_data.zishucup__ruwo_weapon) do
        if player.dead then break end
        if table.contains(player:getCardIds("h"), d) and table.find(player.room.alive_players, function (p)
          return p:canMoveCardIntoEquip(d, false) end) then
          local tos = player.room:askToChoosePlayers(player, {
            min_num = 1,
            max_num = 1,
            targets = table.filter(player.room.alive_players, function (p) return p:canMoveCardIntoEquip(d, false) end),
            skill_name = zishucup__ruwo.name,
            cancelable = false,
            prompt = "#zishucup__ruwo-delay:::"..Fk:getCardById(d):toLogString(),
          })
          player.room:moveCardIntoEquip(tos[1], d, self.name, false, player)
        end
      end
    end
  end,
})


zishucup__ruwo:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.moveReason == fk.ReasonGive and move.skillName == "collateral_skill" then
        local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
        if use_event ~= nil then
          local use = use_event.data
          if (use.extra_data or {}).zishucup__ruwo_user == player.id then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        if player.room:getCardArea(info.cardId) == Card.PlayerHand and Fk:getCardById(info.cardId).sub_type == Card.SubtypeWeapon then
          local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
          if use_event ~= nil then
            local use = use_event.data
            if (use.extra_data or {}).zishucup__ruwo_user == player.id then
              use.extra_data = use.extra_data or {}
              use.extra_data.zishucup__ruwo_weapon = use.extra_data.zishucup__ruwo_weapon or {}
              table.insert(use.extra_data.zishucup__ruwo_weapon, info.cardId)
            end
          end
         end
      end
    end
  end,
})

return zishucup__ruwo
